<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form" label-width="120px">
        <div class="flex">
          <div>
            <div>
              <el-form-item label="手机号">
                <el-input v-model="form.phone" placeholder="手机号" />
              </el-form-item>
              <el-form-item label="Facebook登陆ID">
                <el-input v-model="form.fb_uid" placeholder="Facebook登陆ID" />
              </el-form-item>
            </div>
            <div>
              <el-form-item label="Apple登陆ID">
                <el-input v-model="form.apple_uid" placeholder="Apple登陆ID" />
              </el-form-item>
              <el-form-item label="Google登陆ID">
                <el-input
                  v-model="form.google_uid"
                  placeholder="Google登陆ID"
                />
              </el-form-item>
            </div>
          </div>
          <div>
            <el-button type="primary" @click="onQuery">查询</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" plain @click="onExport">导出</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="用户UID" align="center" />
          <el-table-column prop="user_nick" label="昵称" align="center" />
          <el-table-column
            prop="guest_id"
            label="游客ID"
            align="center"
            width="200"
          />
          <el-table-column
            prop="phone_num"
            label="手机号"
            align="center"
            width="120"
          />
          <el-table-column
            prop="fb_uuid"
            label="Facebook登陆ID"
            align="center"
            width="150"
          />
          <el-table-column
            prop="apple_uuid"
            label="Apple登陆ID"
            align="center"
            width="150"
          />
          <el-table-column
            prop="google_uuid"
            label="Google登陆ID"
            align="center"
            width="150"
          />
          <el-table-column
            prop="tiw_uuid"
            label="Twitter登陆ID"
            align="center"
            width="150"
          />
          <el-table-column
            prop="device_id"
            label="设备ID"
            align="center"
            width="200"
          />
          <el-table-column
            prop="guest_flag"
            width="150"
            label="是否支持游客登陆"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="phone_flag"
            width="150"
            label="是否支持手机登陆"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="fb_flag"
            width="150"
            label="是否支持Facebook登陆"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="apple_flag"
            width="150"
            label="是否支持Apple登陆"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="google_flag"
            width="150"
            label="是否支持Google登陆"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="tiw_flag"
            width="150"
            label="是否支持Twitter登陆"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="old_uid"
            label="用户旧UID"
            align="center"
            width="200"
          />
          <el-table-column
            prop="created_at"
            label="创建时间"
            align="center"
            width="200"
            :formatter="formatterUtcTime"
            
          />
          <el-table-column
            prop="update_at"
            label="更新时间"
            align="center"
            width="200"
            :formatter="formatterUtcTime"
           
          />
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatter ,formatterUtcTime} from "@/utils/utils";
import { usersApi } from "@/api/sdk/sdk";
import { exportToExcel } from "@/utils/utils";
const tableData = ref([]);

const formData = {
  phone: "",
  fb_uid: "",
  apple_uid: "",
  google_uid: "",
};
const form = reactive({ ...formData });
const loading = ref(false);
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    phone: form.phone,
    fb_uid: form.fb_uid,
    apple_uid: form.apple_uid,
    google_uid: form.google_uid,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await usersApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await usersApi(data);

  let head = {
    id: "用户UID",
    user_nick: "昵称",
    guest_id: "游客ID",
    phone_num: "手机号",
    fb_uuid: "Facebook登陆ID",
    apple_uuid: "Apple登陆ID",
    google_uuid: "Google登陆ID",
    tiw_uuid: "Twitter登陆ID",
    device_id: "设备ID",
    guest_flag: "是否支持游客登陆",
    phone_flag: "是否支持手机登陆",
    fb_flag: "是否支持Facebook登陆",
    apple_flag: "是否支持Apple登陆",
    google_flag: "是否支持Google登陆",
    tiw_flag: "是否支持Twitter登陆",
    old_uid: "用户旧UID",
    created_at: "创建时间",
    update_at: "更新时间",
  };
  exportToExcel(res.data.results, head, "SDK-用户管理");
 
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
